package com.gitee.zw.irmp.common.utils;

import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import org.jasypt.encryption.StringEncryptor;
import org.springframework.beans.factory.annotation.Value;

import static com.google.common.base.Charsets.UTF_8;


public class SM4Encryptor implements StringEncryptor {

    @Value("${jasypt.encryptor.password}")
    private String salt;

    @Override
    public String encrypt(String message) {
        SymmetricCrypto sm4 = SmUtil.sm4(salt.getBytes());
        String encryptHex = sm4.encryptHex(message);
        return encryptHex;
    }

    @Override
    public String decrypt(String encryptedMessage) {
        SymmetricCrypto sm41 = SmUtil.sm4(salt.getBytes());
        String decryptStr = sm41.decryptStr(encryptedMessage, UTF_8);
        return decryptStr;
    }

    public static void main(String[] s) {
        SM4Encryptor enc = new SM4Encryptor();
        enc.salt = "ya024uspkidtclu3";
        String encrypt = enc.encrypt("Enmo@123");
        System.out.println(encrypt);
    }
}
